package dialogs
{
	import UIFree.components.uif_window;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import components.pj_button;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class pi_alert extends uif_window {
		protected var _desc: uif_label;
		protected var _button: uif_label;
		
		public function pi_alert(parent:uif_component, message: String, closeFunc: Function=null, cancelButton: Boolean = false, canClose:int=2) {
			var size: Point = getSize();
			super(parent, 0, 0, size.x, size.y, 'window', true, canClose);
			createComponents(message, closeFunc, cancelButton);
		}
		
		protected function getSize(): Point {
			return new Point(420, 200);
		}
		
		protected function createComponents(message: String, closeFunc: Function=null, cancelButton: Boolean = false): void {
			var inner: Rectangle = getInner();
			_desc = new uif_label(this, inner.x, inner.y, inner.width, 20, 'description');
			_desc.htmlText = message;
			inner = resizeFromText();
			toCenter(app);
			
			_button = new pj_button(this, inner.x, height - pj_button.HEIGHT, 80, 'Оk', function(e: MouseEvent): void {
				if (closeFunc != null) {
					function doCloseAlert(e: Event): void {
						removeEventListener(uif_window.CLOSEWINDOW, doCloseAlert);
						closeFunc();
					}
					addEventListener(uif_window.CLOSEWINDOW, doCloseAlert);
				}
				doClose(e);
			});
			if (cancelButton) {
				 var _cancel: uif_label = new pj_button(this, inner.x, height - pj_button.HEIGHT, 80, 'Отмена', doClose);
				 _button.x = inner.x + inner.width / 2 - _button.width;
				 _cancel.x = inner.x + inner.width / 2;
			} else _button.x = (inner.width - _button.width) / 2;
		}
		
		protected function resizeFromText(): Rectangle {
			_desc.textField.height = _desc.textField.textHeight;
			_desc.height = _desc.textField.height + _desc.getSpace(true) * 2;
			height = _desc.height + getSpace(true) * 2 + 35; 
			return getInner();
		}
		
		public function get okButton(): uif_label {
			return _button;
		}
		
		
	}
}